/*
 * Flame.cpp
 *
 *  Created on: 22-08-2012
 *      Author: szymon
 */

#include "Flame.h"
#include "SurfaceOps.h"
#include <cassert>

Flame::Flame(Screen * screen, int offsetX, int offsetY, enum Flame::Direction direction)
{
	this->screen = screen;
	this->offsetX = offsetX;
	this->offsetY = offsetY;
	this->direction = direction;
	velocity = direction == right ? FLAME_WIDTH/6 : -(FLAME_WIDTH/6);
	frame = 0;
	//Load the sprite sheet
	flameSurface = SurfaceOps::loadImage("flame.png");
	assert(flameSurface != NULL);
	setClips();
}

Flame::~Flame()
{
	SDL_FreeSurface(flameSurface);
}

void Flame::setClips(void)
{
	for (int i = 0; i < FLAME_SPRITES_NUM; i++)
	{
		//Clip the sprites
		clipsRight[i].x = 0 + FLAME_WIDTH * i;
		clipsRight[i].y = 0;
		clipsRight[i].w = FLAME_WIDTH;
		clipsRight[i].h = FLAME_HEIGHT;
	}

	for (int i = 0; i < FLAME_SPRITES_NUM; i++)
	{
		//Clip the sprites
		clipsLeft[FLAME_SPRITES_NUM - 1 - i].x = 0 + FLAME_WIDTH * i;
		clipsLeft[FLAME_SPRITES_NUM - 1 - i].y = FLAME_HEIGHT;
		clipsLeft[FLAME_SPRITES_NUM - 1 - i].w = FLAME_WIDTH;
		clipsLeft[FLAME_SPRITES_NUM - 1 - i].h = FLAME_HEIGHT;
	}
}

void Flame::move()
{
	offsetX += velocity;

	if ((offsetX + FLAME_WIDTH < 0) || (offsetX - FLAME_WIDTH >= SCREEN_WIDTH))
	{
		velocity = 0;
	}
}

void Flame::show()
{
	if(direction == right)
	{
		SurfaceOps::applySurface(offsetX, offsetY, flameSurface, screen->surface(), &clipsRight[frame]);
	}
	else
	{
		SurfaceOps::applySurface(offsetX, offsetY, flameSurface, screen->surface(), &clipsLeft[frame]);
	}
}

bool Flame::finished()
{
	return (velocity == 0);
}
